package com.feicheng.controller.ai;


import com.feicheng.common.result.ResultInfo;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.AbstractChatMemoryAdvisor;
import org.springframework.ai.chat.client.advisor.PromptChatMemoryAdvisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.memory.InMemoryChatMemory;
import org.springframework.ai.ollama.OllamaChatModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.CoreSubscriber;
import reactor.core.publisher.Flux;

import java.awt.*;
import java.nio.charset.Charset;
import java.time.LocalDateTime;

@RestController
@RequestMapping("/admin/ts")
public class TsController {

    @Autowired
    private OllamaChatModel ollamaChatModel;

    ChatClient chatClient;
    private ChatMemory cm = new InMemoryChatMemory();

    public TsController(ChatClient.Builder builder) {
        this.chatClient = builder
                .defaultSystem("您是飞程公司的库管员，您正在通过在线聊天系统与客户互动。请讲中文。今天的日期是{current_date}。")
                .defaultAdvisors(new PromptChatMemoryAdvisor(cm))
                .build();

    }

    @GetMapping("/dp")
    public ResultInfo test(String con){
        String r = ollamaChatModel.call(con);
        return ResultInfo.success(r);
    }

//    @GetMapping(value="/al", produces = "text/event-stream;charset=utf-8")
//    public Flux<String> al(String con) {
//        Flux<String> result =
//                this.chatClient.prompt()
//                .user(con)
//                .system(promptSystemSpec -> promptSystemSpec.param("current_date", LocalDateTime.now().toString()))
//                .stream()
//                .content();
//        return result.concatWith(Flux.just("嘎哈")).concatWith(Flux.just("shazi")).concatWith(Flux.just("[end]"));
//    }

    @GetMapping(value="/al", produces = "application/json;charset=utf-8")
    public String al(String con) {
        String result =
                this.chatClient.prompt()
                        .user(con)
                        .system(promptSystemSpec -> promptSystemSpec.param("current_date", LocalDateTime.now().toString()))
                        .advisors(advisorSpec -> advisorSpec.param(AbstractChatMemoryAdvisor.CHAT_MEMORY_RETRIEVE_SIZE_KEY, 100))
                        .call().content();
        return result;
    }

}
